/**
 * 
 */
package com.java.test.paymanage.mapper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.paymanage.dto.PayChannelInfo;
import com.java.test.user.dto.DrawingTypeInfo;

/**
 * @author Administrator
 *
 */
@Component
public class PayChannelMapper {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<PayChannelInfo> selectpaychanneList(PayChannelInfo info) {

		String sql = "select a.id,a.pay_type,a.channel_name,a.add_people_name,"
				+ "a.add_time,a.content,a.iconUrl,a.sort,a.gfreal_name,"
				+ "b.pay_type_name as pay_type_name from paymanage_channel a "
				+ "left join paymanage_paytype b on a.pay_type=b.id  where 1=1 ";

		if (info.getPayType() != 0) {
			sql += " and pay_type='" + info.getPayType() + "'";
		}

		sql += " order by a.sort desc ";

		List<PayChannelInfo> list = jdbcTemplate.query(sql, new PayChannelInfo());

		return list;
	}

	public int insertpayChannelInfo(PayChannelInfo info) {

		String sql = "insert into paymanage_channel(pay_type,pay_type_name,"
				+ "channel_name,add_people_name,add_time,content,iconUrl,sort,gfreal_name)"
				+ "values(?,?,?,?,?,?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getPayType(), info.getPayTypeName(), info.getChannelName(),
				info.getAddPeopleName(), info.getAddTime(), info.getContent(), info.getIconUrl(), info.getSort(),
				info.getGfrealmname());

		return i;
	}

	public PayChannelInfo selectChannelInfobyid(int id) {

		String sql = "select * from paymanage_channel  where id =?";
		PayChannelInfo info = null;

		try {
			info = jdbcTemplate.queryForObject(sql, new PayChannelInfo(), id);
		} catch (Exception e) {

			return null;
		}
		return info;
	}

	public int updatepayChannelInfo(PayChannelInfo info) {

		String sql = "update paymanage_channel set channel_name=?,pay_type=?,content=?,iconUrl=?,gfreal_name=? where id =?";

		int i = jdbcTemplate.update(sql, info.getChannelName(), info.getPayType(), info.getContent(), info.getIconUrl(),
				info.getGfrealmname(), info.getId());

		return i;
	}

	public int deletepayChannelInfo(PayChannelInfo info) {

		String sql = "delete from paymanage_channel  where id =?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;
	}

	public int updatepayChannelInfosort(List<PayChannelInfo> info) {

		String sql = "update paymanage_channel set sort=? where id =?";

		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return info.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, info.get(i).getSort());
				ps.setInt(2, info.get(i).getId());
			}
		});

		return 1;
	}

	public PayChannelInfo selectpaychannelbyId(int id) {

		String sql = "select * from paymanage_channel where id=?";
		PayChannelInfo info = null;
		try {
			info = jdbcTemplate.queryForObject(sql, new PayChannelInfo(), id);
		} catch (Exception e) {
			return null;
		}
		return info;

	}

	public List<PayChannelInfo> selectpaychannelbypaytypeId(int paytypeid) {

		String sql = "select * from paymanage_channel where pay_type=?";

		List<PayChannelInfo> list = jdbcTemplate.query(sql, new PayChannelInfo(), paytypeid);

		return list;

	}

}
